﻿using System;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ProfileManagement
{
    public partial class AddClient : System.Web.UI.Page
    {
        private ClientEntity _client;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();
        }

        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            if (checkAutoAccount.Checked)
            {
                var userName = AdministrationUtilityLogic.AutoUserName(textFullName.Text);
                // Add new account for contractor
                var accountEntity = new AccountEntity
                                        {
                                            UserName = userName,
                                            Password = "123456",
                                            // Default password
                                            RoleId = (int)RoleList.LineManager,
                                            Status = 1,
                                            ActorType = "LineManager"
                                        };
                var result = AdministrationUtilityLogic.AddNewAccount(accountEntity);

                _client = new ClientEntity
                              {
                                  UserName = userName,
                                  FullName = Server.HtmlEncode(textFullName.Text),
                                  CompanyName = Server.HtmlEncode(textCompanyName.Text),
                                  Address = Server.HtmlEncode(textAddress.Text),
                                  PhoneNumber = Server.HtmlEncode(textPhoneNumber.Text),
                                  Email = Server.HtmlEncode(textEmail.Text)
                              };

                var insert = ClientProfileLogic.InsertClient(_client);

                if (insert == "Successfully!" && result == "Successfully!")
                {
                    //redirect to page
                    Response.Redirect("ListClient.aspx");
                }

                UserEventEntity(insert);
            }
            else
            {
                _client = new ClientEntity
                              {
                                  UserName = "",
                                  FullName = Server.HtmlEncode(textFullName.Text),
                                  CompanyName = Server.HtmlEncode(textCompanyName.Text),
                                  Address = Server.HtmlEncode(textAddress.Text),
                                  PhoneNumber = Server.HtmlEncode(textPhoneNumber.Text),
                                  Email = Server.HtmlEncode(textEmail.Text)
                              };

                var insert = ClientProfileLogic.InsertClient(_client);

                if (insert == "Successfully!")
                {
                    //redirect to page
                    Response.Redirect("ListClient.aspx");
                }

                UserEventEntity(insert);

            }

        }

        //event of insert client
        private void UserEventEntity(string userEvent)
        {
            // Save event of insert client
            var userEventEntity = new EventEntity
            {
                UserName = CommonLogic.ConvertToString(Session["UserName"]),
                EventType = (int)EventTypeList.UserEvent,
                OnDate = DateTime.Now,
                Description = "User Insert client "+userEvent,
            };
            EventTracer.SaveEvents(userEventEntity);
            ResultMessage.Visible = true;
            ResultMessage.Text = userEvent;
        }

        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int) FunctionList.AddClient;

            var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");

            AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
        }
        //reset text emtry
        protected void ButtonResetClick(object sender, EventArgs e)
        {
            textFullName.Text = string.Empty;
            textAddress.Text = string.Empty;
            textCompanyName.Text = string.Empty;
            textPhoneNumber.Text = string.Empty;
            textEmail.Text = string.Empty;
        }
    }
}